package edu.bw.web.user;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.SelectUserByConditionForm;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import edu.bw.utils.PageUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/user/selectUserByCondition")
public class SelectUserByConditionServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{"page":1,"length":10,"name":null,"sex":"男","role":"","deptId":"","status":""}
        String reqJSON = request.getReader().readLine();
        SelectUserByConditionForm selectCondition = JSONUtil.toBean(reqJSON, SelectUserByConditionForm.class);

        //调用service层处理数据
        PageUtils pageUtils = userService.selectUserByCondition(selectCondition);

        //向前端显示结果
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("page", pageUtils)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
